package com.seven.dlandemo.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.seven.dlandemo.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    public static final String myTAG = "sevenxue ";
    private String testVideoUrl = "https://www.apple.com/105/media/cn/iphone-xs/2018/674b340a-40f1-4156-bbea-00f386459d3c/films/design/iphone-xs-design-tpl-cn-2018_1280x720h.mp4";

    private EditText hostHeadEt;
    private EditText roomIdEt;
    private RadioGroup rateGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        hostHeadEt = findViewById(R.id.host_head);
        roomIdEt = findViewById(R.id.room_id_or_url);
        if (roomIdEt != null) {
            roomIdEt.requestFocus();
        }
        rateGroup = findViewById(R.id.rate);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.wireless_display:
                startWirelessDisplay();
                break;
        }
    }

    private void startWirelessDisplay(){
        if (roomIdEt != null){
            String input = roomIdEt.getText().toString();
            if (!TextUtils.isEmpty(input)){
                String url = null;
                if (input.startsWith("http")) {
                    // 输入完整的URL
                    url = input;
                }else {
                    // 输入房间id、主播
                    String hostHead = "";
                    String roomId = "";
                    int rate = 3000;
                    if (hostHeadEt != null) {
                        hostHead = hostHeadEt.getText().toString();
                    }
                    if (roomIdEt != null) {
                        roomId = roomIdEt.getText().toString();
                    }
                    if (rateGroup != null) {
                        switch (rateGroup.getCheckedRadioButtonId()){
                            case R.id.rate_900:
                                rate = 900;
                                break;
                            case R.id.rate_1500:
                                rate = 1500;
                                break;
                            case R.id.rate_3000:
                                rate = 3000;
                                break;
                            case R.id.rate_6000:
                                rate = 6000;
                                break;
                        }
                    }
                    url = String.format("http://%s.liveplay.myqcloud.com/live/%s_%s_%d.flv", hostHead, hostHead, roomId, rate);
        //                              "http://3954.liveplay.myqcloud.com/live/3954_430327538_3000.flv"
                }
                if (url != null) {
                    WirelessDisplayActivity.start(this, url);
                }
            }else {
                WirelessDisplayActivity.start(this, testVideoUrl);
            }
        }
    }
}
